#include "ivstate.h"

using namespace  std;
IVTState::IVTState()
{
    m_state = 0;//默认状态为IDEL
}

void IVTState::iniWay()
{
    int ok;

    std::cout<<"IDEl can to RUNNING?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(01);
    }


    std::cout<<"IDEl can to FAILED?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(02);
    }


    std::cout<<"IDEl can to SUCCESSFUL?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(03);
    }


    std::cout<<"RUNNING can to IDEl?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(10);
    }


    std::cout<<"RUNNING can to FAILED?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(12);
    }


    std::cout<<"RUNNING can to SUCCESSFUL?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(13);
    }


    std::cout<<"FAILED can to IDEl?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(20);
    }


    std::cout<<"FAILED can to RUNNING?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(21);
    }


    std::cout<<"FAILED can to SUCCESSFUL?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(23);
    }


    std::cout<<"SUCCESSFUL can to IDEl?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(30);
    }


    std::cout<<"SUCCESSFUL can to RUNNING?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(31);
    }


    std::cout<<"SUCCESSFUL can to FAILED?\n";
    cin>>ok;
    if(ok == 1){
        m_list.push_back(32);
    }

     std::cout<<"init finish!\n";
     ofstream fout;
     fout.open("information.txt",ios::out);

    fout<<"init finish!\n";
    fout.close();
}

int IVTState::changeState()
{
    int nextState;
    std::cout<<"\n\nplease press next state...(press 6 to quit.)\n";
    cin>>nextState;
    if(nextState == 6){
        return nextState;
    }
    int stateChang = m_state*10+nextState;
    bool ok =false;
    for(std::list<int>::iterator i = m_list.begin();i != m_list.end();++i){
        if(*i == stateChang){
            ok =true;
            break;
        }
    }

    ofstream fout;
    fout.open("information.txt",ios::app);
    if(ok == true){

        cout<<"success!from "<<m_state<<" to "<<nextState<<"\n";

        fout<<"success!from "<<m_state<<" to "<<nextState<<"\n";
        fout.close();

        m_state = nextState;
    }else{
        cout<<"fail! "<<m_state<<" can not to "<<nextState<<endl;

       fout<<"success!from "<<m_state<<" to "<<nextState<<"\n";
       fout.close();

    }
    return 0;
}
